package admin

import (
	"context"
	"diygw-ui-go/api/v1/admin"
	"diygw-ui-go/internal/v1/bll"
	bll_admin "diygw-ui-go/internal/v1/model/admin"
	"diygw-ui-go/internal/v1/service"
	"github.com/gogf/gf/v2/util/gconv"
)

// 用户管理
var SysUser = sysUser{}

type sysUser struct{}

func (c *sysUser) Login(ctx context.Context, req *admin.LoginDoReq) (res admin.LoginDoRes[any], err error) {
	_rt_model := struct {
		bll_admin.SysUserOneOutput
		Password string   `json:"password,omitempty"   ` // 密码
		Salt     string   `json:"salt,omitempty"       ` // 加盐
		Token    string   `json:"token"`                 //token值
		Roles    []string `json:"roles"`                 //角色列表
		Auths    []string `json:"auths"`                 //权限列表
	}{}
	_model, err := service.SysUser().Login(ctx, req.SysUserLoginInput)
	if err != nil {
		res.Res = bll.Fail
		res.Res.Msg = err.Error()
	} else {
		res.Res = bll.Success
		_rt_model.SysUserOneOutput = _model
		_rt_model.Token = "bearereyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjEsImF1ZCI6IiIsImV4cCI6MTcxMDkxODcxNywiaWF0IjoxNzEwMzEzOTE3LCJpc3MiOiIiLCJqdGkiOiI0MGNjMDVkZGQxNzk0ZDEzODE1ZDcxNjY5NzFhM2MwYSIsIm5iZiI6MTcxMDMxMzkxNywic3ViIjoiIn0.6bjqLcG4mZQ0gSBhVjK_mdKy8e1Am0tU-GT41AYfmXE"
		_list_all, _ := service.SysRole().GetList(ctx, bll_admin.SysRolelistInput{RoleIds: gconv.Int64s(_model.RoleIds)})
		for _, _item := range _list_all.List {
			_rt_model.Roles = append(_rt_model.Roles, _item.RoleKey)
			_auths, _ := service.SysRole().GetAuths(ctx, _item.RoleId)
			for _, _auth := range _auths {
				_rt_model.Auths = append(_rt_model.Auths, _auth)
			}
		}
		res.Data = &_rt_model
		res.Res.Msg = "登录成功"
	}
	res.RtJs(ctx, res)
	return
}
